package com.sgcc.nfc.lock.presentation.locks

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemLockBinding
import com.sgcc.nfc.lock.domain.model.Lock
import com.sgcc.nfc.lock.domain.model.LockStatus

class LockAdapter(
    private val onItemClick: (Lock) -> Unit,
    private val onDeleteClick: ((Lock) -> Unit)? = null
) : ListAdapter<Lock, LockAdapter.LockViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LockViewHolder {
        val binding = ItemLockBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return LockViewHolder(binding)
    }

    override fun onBindViewHolder(holder: LockViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class LockViewHolder(
        private val binding: ItemLockBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Lock) {
            val ctx = binding.root.context
            
            // 设置锁具名称
            binding.lockNameText.text = item.lockName
            
            // 设置配电室信息
            binding.lockRoomText.text = item.roomName ?: ctx.getString(R.string.lock_room_unknown)
            
            // 设置NFC ID
            binding.lockNfcIdText.text = item.lockNfcId
            
            // 设置状态芯片
            binding.lockStatusChip.apply {
                text = when (item.status) {
                    LockStatus.CLOSED -> ctx.getString(R.string.lock_status_closed)
                    LockStatus.OPEN -> ctx.getString(R.string.lock_status_open)
                    LockStatus.MALFUNCTION -> ctx.getString(R.string.lock_status_malfunction)
                    LockStatus.UNKNOWN -> ctx.getString(R.string.lock_status_unknown)
                }
                
                // 设置芯片颜色
                val chipBackgroundColor = when (item.status) {
                    LockStatus.CLOSED -> R.color.color_status_success_container
                    LockStatus.OPEN -> R.color.color_status_warning_container
                    LockStatus.MALFUNCTION -> R.color.color_status_error_container
                    LockStatus.UNKNOWN -> R.color.color_status_neutral_container
                }
                
                val chipTextColor = when (item.status) {
                    LockStatus.CLOSED -> R.color.color_status_success
                    LockStatus.OPEN -> R.color.color_status_warning
                    LockStatus.MALFUNCTION -> R.color.color_status_error
                    LockStatus.UNKNOWN -> R.color.color_status_neutral
                }
                
                setChipBackgroundColorResource(chipBackgroundColor)
                setTextColor(ContextCompat.getColor(ctx, chipTextColor))
            }
            
            // 设置锁具图标
            binding.lockIcon.setImageResource(
                when (item.status) {
                    LockStatus.CLOSED -> R.drawable.ic_lock_24
                    LockStatus.OPEN -> R.drawable.ic_lock_open_24
                    LockStatus.MALFUNCTION -> R.drawable.ic_lock_alert_24
                    LockStatus.UNKNOWN -> R.drawable.ic_lock_24
                }
            )
            
            // 设置点击事件
            binding.root.setOnClickListener { onItemClick(item) }
            
            // 设置删除按钮
            binding.deleteButton.apply {
                isVisible = onDeleteClick != null
                setOnClickListener {
                    onDeleteClick?.invoke(item)
                }
            }
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<Lock>() {
        override fun areItemsTheSame(oldItem: Lock, newItem: Lock): Boolean = oldItem.lockId == newItem.lockId
        override fun areContentsTheSame(oldItem: Lock, newItem: Lock): Boolean = oldItem == newItem
    }
}
